package com.hq.fxh.CustomerView.PopupWindow;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hq.fxh.No3_TuiGuang.MyMoBan.Adapter.MySpringAdapter;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;


/**
 * Created by Administrator on 2017/2/16.
 */
public class PopupForList extends PopupWindow implements View.OnClickListener {


    protected View rootView;

    protected ListView popList;
    protected LinearLayout popupPingBi;
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    private Activity context;
    private View popView;
    ShareModleCallBack modleCallBack;
    String[] data;
    MySpringAdapter adapter;

    public PopupForList(Activity context, String[] data, ShareModleCallBack modleCallBack) {
        super(context);
        this.context = context;
        this.data = data;
        this.modleCallBack = modleCallBack;
        popView = LayoutInflater.from(context).inflate(R.layout.popup_list, null);
        initView(popView);
        initPoP();
        initData();
        settitle(null);

    }

    private void initPoP() {

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String data = adapterView.getItemAtPosition(i).toString();
                LogUtils.e(data);
                modleCallBack.callSure(i, data);
            }
        });
    }


    public void settitle(String title) {
        if (title == null) {
            toolbarContent.setVisibility(View.GONE);
        } else {
            toolbarContent.setVisibility(View.VISIBLE);
            toolBarTvTitle.setText(title);
        }

    }

    public void setData(String[] data) {
        this.data = data;
        adapter.setData(data);
        adapter.notifyDataSetChanged();

    }

    public void initData() {

        //设置PopupWindow的View
        this.setContentView(popView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setAnimationStyle(R.style.dialog_anim_style);
        this.setOutsideTouchable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
    /*    openKeyboard();*/

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha(context, 1f);

            }
        });

    }


    private void initView(View rootView) {

        popList = (ListView) rootView.findViewById(R.id.pop_list);
        popupPingBi = (LinearLayout) rootView.findViewById(R.id.popup_PingBi);
        adapter = new MySpringAdapter();
        adapter.setData(data);
        popList.setAdapter(adapter);

        toolbarImageBack = (ImageView) rootView.findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(PopupForList.this);
        toolBarTvTitle = (TextView) rootView.findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) rootView.findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_More);
        toolBarTvMore.setVisibility(View.INVISIBLE);
        toolbarContent = (LinearLayout) rootView.findViewById(R.id.toolbar_Content);


    }


    public void show(View view) {

        showAtLocation(view, Gravity.BOTTOM, 0, 0);
        Tool.setAlpha(context, 0.7f);
    }

    public void showCenter(View view) {

        showAtLocation(view, Gravity.CENTER, 0, 0);
        Tool.setAlpha(context, 0.7f);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.toolbar_Layout_back) {
            dismiss();
        }
    }

    public interface ShareModleCallBack {
        public void callSure(int position, String comment);

    }


}
